package dst1.listener;

import javax.persistence.PostLoad;
import javax.persistence.PostPersist;
import javax.persistence.PostRemove;
import javax.persistence.PostUpdate;
import javax.persistence.PrePersist;

public class DefaultEntityListener {
	static int persist;
	static int load;
	static int update;
	static int delete;
	static long now;
	static long totalPersistTime;
	
	@PrePersist
	public static void onPrePersist(Object o) {
		now = System.currentTimeMillis();
	}
	
	@PostPersist
	public static void onPostPersist(Object o) {
		persist++;

		totalPersistTime += System.currentTimeMillis() - now;
	}

	@PostLoad
	public static void onPostLoad(Object o) {

		load++;
	}

	@PostUpdate
	public static void onPostUpdate(Object o) {

		update++;
	}

	@PostRemove
	public static void onPostRemove(Object o) {

		delete++;
	}

	public static int getPersist() {
		return persist;
	}

	public static int getLoad() {
		return load;
	}

	public static int getUpdate() {
		return update;
	}

	public static int getDelete() {
		return delete;
	}
	
	public static long getTotalPersistTime() {
		return totalPersistTime;
	}
	
	public static long getAveragePersistTime() {
		return totalPersistTime / persist;
	}
}
